#include "candrive.h"
#include <qDebug>
#include <cstring>

CanDrive::CanDrive()
{

}

DWORD CanDrive::OpenCAN()
{
    DWORD ret = VCI_OpenDevice(device_type, device_index, reserved);
    return ret;
}

DWORD CanDrive::InitCAN(int baundrate)
{
    VCI_INIT_CONFIG vic;
    vic.AccCode = 0x00000000;    // 滤波验收码
    vic.AccMask = 0xFFFFFFFF;    // 滤波屏蔽码：0xFFFFFFFF接收所有消息
    vic.Filter = 1;               // 滤波模式：0/1接收所有类型、2只接收标准帧、3只接收扩展帧
    vic.Mode = 0;                 // 工作模式：0正常工作、1监听模式、2测试模式(自发自收)
    switch (baundrate) {
    case 10:
        vic.Timing0 = 0x31;       // 波特率T0设置
        vic.Timing1 = 0x1c;       // 波特率T1设置
        break;
    case 20:
        vic.Timing0 = 0x18;
        vic.Timing1 = 0x1c;
        break;
    case 40:
        vic.Timing0=0x87;
        vic.Timing1=0xff;
        break;
    case 50:
        vic.Timing0 = 0x09;
        vic.Timing1 = 0x1c;
        break;
    case 80:
        vic.Timing0 = 0x83;
        vic.Timing1 = 0xff;
        break;
    case 100:
        vic.Timing0 = 0x04;
        vic.Timing1 = 0x1c;
        break;
    case 125:
        vic.Timing0 = 0x03;
        vic.Timing1 = 0x1c;
        break;
    case 200:
        vic.Timing0 = 0x81;
        vic.Timing1 = 0xfa;
        break;
    case 250:
        vic.Timing0 = 0x01;
        vic.Timing1 = 0x1c;
        break;
    case 400:
        vic.Timing0 = 0x80;
        vic.Timing1 = 0xfa;
        break;
    case 500:
        vic.Timing0 =0x00;
        vic.Timing1 = 0x1c;
        break;
    case 666:
        vic.Timing0 = 0x80;
        vic.Timing1 = 0xb6;
        break;
    case 800:
        vic.Timing0 = 0x00;
        vic.Timing1 = 0x16;
        break;
    case 1000:
        vic.Timing0 = 0x00;
        vic.Timing1 = 0x14;
        break;
    case 33:
        vic.Timing0 = 0x09;
        vic.Timing1 = 0x6f;
        break;
    case 66:
        vic.Timing0 = 0x04;
        vic.Timing1 = 0x6f;
        break;
    case 83:
        vic.Timing0 = 0x03;
        vic.Timing1 = 0x6f;
        break;
    default:
        break;
    }
    DWORD ret =  VCI_InitCAN(device_type, device_index, can_chanel, &vic);

    return ret;
}

DWORD CanDrive::ClearBuffer()
{
    DWORD ret = VCI_ClearBuffer(device_type, device_index, can_chanel);
    return ret;
}

DWORD CanDrive::GetBoardInfo()
{
    DWORD ret = VCI_ReadBoardInfo(device_type, device_index, vbi);
    qDebug()<< vbi->fw_Version ;
    return ret;
}

DWORD CanDrive::StartCAN()
{
    DWORD ret = VCI_StartCAN(device_type, device_index, can_chanel);
    return ret;
}

DWORD CanDrive::CloseCAN()
{
    DWORD ret = VCI_CloseDevice(device_type,device_index);
    return ret;
}

int32_t CanDrive::canTransmitData(uint32_t id ,uint8_t len ,uint8_t *buf)
{
    VCI_CAN_OBJ canSendObj ;
    canSendObj.ID = id;           // SDO 发送指令600H + NodeID
    canSendObj.SendType = 1;
    canSendObj.RemoteFlag = 0;
    canSendObj.ExternFlag = 0;
    canSendObj.DataLen = len;             // 发送字节长度
    for(int i = 0; i < canSendObj.DataLen; i++)
    {
        canSendObj.Data[i] =  buf[i];
    }
    int32_t ret = VCI_Transmit(device_type, device_index, can_chanel, &canSendObj, 1);
    return ret;
}

int32_t CanDrive::canReceiveData(void)
{
    int ret = 0 ;
    ret = VCI_Receive(device_type, device_index, can_chanel, &canRecvObj[0], 1024,0);
    if(ret == -1)
    {
        qDebug()<< "can 读取错误";
    }
    for(int i = 0 ; i < ret ; i++)
    {
        frame_queue.enqueue(canRecvObj[i]);
    }
    return ret;
}
